﻿#ifndef _FDSET_H
#define _FDSET_H

#include "socketlibtypes.h"

#if defined PLATFORM_LINUX
#include "basetype.h"
#endif

namespace SocketLib
{

class SocketSet
{
public:
    enum ActiveType
    {
        ReadActive = 0x1,
        WriteActive = 0x2,
        ErrorActive = 0x4,
    };

public:
    SocketSet();
    ~SocketSet();
    void            addSock(sock aSock, int aType = int(ReadActive | WriteActive | ErrorActive));
    void            delSock(sock aSock, int aType = int(ReadActive | WriteActive | ErrorActive));

    int             poll(long aOverTime);

    bool            isActive(sock aSock, ActiveType    aType);

    fd_set&         getReadFDSet();
    fd_set&         getWriteFDSet();

#if defined PLATFORM_LINUX
    void            pushSock(sock);
    void            eraseSock(sock);
#endif

protected:

    // 保存fd_set原始设置
    fd_set            _readSet;
    fd_set            _writeSet;
    fd_set            _errorSet;

    // 用于select，保存结果
    fd_set            m_readSetResult;
    fd_set            m_writeSetResult;
    fd_set            m_errorSet;

private:
    #if defined PLATFORM_LINUX
    DVector<bool>*  m_socks;
    sock            m_maxSock;
#endif

private:
};

}

#endif
